import axios from 'axios';
import * as types from '../store/types';
import { baseURL,baseURLDownload } from './config.js';
const queryString = require('query-string');

console.log(baseURL);

var crt_axios=axios.create({
	baseURL: baseURL
})
var common_axios=axios.create({})

common_axios.interceptors.request.use(
	(config)=>{
		config.data=queryString.stringify(config.data);
		return config;
	},(error)=>{
		return Promise.reject(error);
	}
);
common_axios.interceptors.response.use(
	(response)=>{
		if(typeof(response.data)=="string"){
			if(response.data.indexOf("loginPage")!=-1){
				window.location.href=baseURL+'/static/login.html';
			}
		}
		return response;
	},(error)=>{
		if(error.response) {
			vm.$message({
				duration:5000,
				type: 'warning',
				message:error.response.status+" "+error.response.statusText
			})
		}
		return Promise.reject(error);
	}
);

crt_axios.interceptors.request.use(
	(config)=>{
		config.data=queryString.stringify(config.data);
		//vm.$store.commit(types.LOADING_COUNT_ADD)
		return config;
	},(error)=>{
		//vm.$store.commit(types.LOADING_COUNT_MINUS)
		return Promise.reject(error);
	}
);
crt_axios.interceptors.response.use(
	(response)=>{
		//vm.$store.commit(types.LOADING_COUNT_MINUS)
		if(typeof(response.data)=="string"){
			if(response.data.indexOf("loginPage")!=-1){
				window.location.href=baseURL+'/static/login.html';
			}
		}
		return response;
	},(error)=>{
		//vm.$store.commit(types.LOADING_COUNT_MINUS)
		if(error.response) {
			vm.$message({
				duration:5000,
				type: 'warning',
				message:error.response.status+" "+error.response.statusText
			})
		}
		return Promise.reject(error);
	}
);

export default{
	//测试数据
	dataTest:function(data,params){
		return crt_axios.post('/data-test1',data,{params:params});
	},
	//-------------------------------
	//登录
	login:function(data,params){
		return crt_axios.post("/login",data,{params:params });
	},
	//退出登录
	outLogin:function(data,params){
		return crt_axios.post("/outLogin",data,{params:params });
	}
}
